require([
  'jquery',
  'pageInit',
  'dataTableFilter',
  'loading',
  'notification',
  'listAsyncOp'
], function($, vamakerPage, VamDataTable, VamLoader, VamOpMsg, VamListOp) {
  $(function() {
    var 
      $dataTable = $('#data-table'),
      dataUrl = VAM.getVar('DATA_LIST_URL'),
      dataRefreshOp = VAM.getVar('LIST_REFRESH_OP_URL'),
      $reportDataThead = $dataTable.find('thead'),
      $loadingWrap = new VamLoader().loader($('#list-loading-wrap')),
    listTable, tableOptions = {};
    // Table settings.
    tableOptions = {
      'ajax': {
        'type': 'post',
        'url': dataUrl
      },
      'columns': [
        {'data': 'id'},
        {'data': 'channel'},
        {'data': 'period'},
        {'data': 'operate'}
      ],
      'columnDefs': [
        {
          'targets': [0,1,2],
          'render': function (data, type, full, meta) {
              return '<p title="' + data + '">' + data + '</p>';
          }
        },
        {
          'targets': -1,
          'render': function (data, type, full, meta) {
            return '<div class="tab-btn-group">'
                  + '<input type="hidden" class="row-info" value="' + full.id + '"/>'
                  + '<button class="btn btn-success btn-xs tab-refresh" data-type="refresh" data-loading-text="<i class=\'fa fa-refresh fa-spin\'></i>" title="' + VAM.j('Update') + '"><i class="fa fa-refresh"></i></button>'
                  + '</div>';
          }
        }
      ],
      'initComplete': function() {
        tableResize = new VamDataTable().columnResize($dataTable);
      }
    };
    $dataTable
    .on('preXhr.dt', function(evt) {
      $loadingWrap.show();
    });
    listTable = new VamDataTable().dataTables($dataTable, tableOptions);
    // After table data list.
    listTable.on('xhr.dt', function(evt) {
      $loadingWrap.fadeOut();
    });

    $dataTable.on('click', 'tbody td .tab-refresh', function(evt) {
      var $me = $(this),
        $rowInfo = $me.siblings('.row-info'),
        opId = $rowInfo.val(),
        opType = $me.data('type'),
        opData;
      evt.preventDefault();
      opData = {
        id: opId,
        type: opType,
        rnd: (new Date()).getTime()
      };
      VamListOp.listOp({
        url: dataRefreshOp,
        data: opData,
        before: function () {
          $me.button('loading');
        },
        success: function (data) {
          if (data.status === 'success') {
            VamOpMsg.messager(data);
            listTable.ajax.reload();
          } else {
            VamOpMsg.messager(data);
          }
        },
        always: function () {
          $me.button('reset');
        }
      });
    });
  });
});
